import User.MessageDTO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.*;


@WebServlet(urlPatterns = {"Register_Check"})
public class Register_Check extends HttpServlet{
    //注册信息写入文件
    protected static void register_write(BufferedWriter bufferedWriter, String name, String email, String password, String phone) {
        String line =name+" "+email+" "+password+" "+phone+" "+"img1.jpg";
        try {
            if (line != null) {
                bufferedWriter.write(line);
                bufferedWriter.newLine();
            }
            bufferedWriter.close();
        } catch (IOException e) {
            System.out.println("写入失败！");
        }
    }
    //判断注册信息是否早已存在
    protected static boolean register_Exit(BufferedReader bufferedReader,String name,String email,String password,String phone) {
        String line = null;
        try {
            while ((line = bufferedReader.readLine()) != null) {
                String[] str = line.split(" ");
                if (str[0].equals(name)) {
                    bufferedReader.close();
                    return false;
                }
            }
        } catch (IOException e) {
            System.out.println("读取失败！");
        }
        return true;
    }


    //判断注册信息是否成功，保证两次输入密码一致
    protected static boolean register_success(String name,String email,String password,String qrpassword,String phone)
    {
        if(!password.equals(qrpassword)||(email.length()<=0)||(name.length()<=0)||(phone.length()<=0)||(password.length()<=0))
        {
            return false;
        }
        return true;
    }

    protected void register_check(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {

        String name = req.getParameter("username");
        String email = req.getParameter("e-mail");
        String password = req.getParameter("password");
        String qrpassword = req.getParameter("qrpassword");
        String phone = req.getParameter("phone");
        if(!register_success(name,email,password,qrpassword,phone)) {
            resp.sendRedirect("error.jsp");
        }
        else{
            try{
                BufferedReader bufferedReader=new BufferedReader(new FileReader("src/main/a.txt"));
                if(register_Exit(bufferedReader,name,email,password,phone))
                {
                    BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("src/main/a.txt",true));
                    register_write(bufferedWriter,name,email,password,phone);
                    bufferedWriter.close();
                    System.out.println("注册成功，登录到用户界面");

                    MessageDTO messageDTO=new MessageDTO(name,email,password,phone,"img1.jpg");
                    req.getSession().setAttribute("LocalUser",messageDTO);
                    resp.sendRedirect("userinfo.jsp");
                }
                else resp.sendRedirect("error.jsp");
            }catch (IOException e)
            {
                resp.sendRedirect("error.jsp");
            }
        }

    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        register_check(req,resp);
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

}
